<template>
  <div>
    <div class="panel" bar>
      <h2>悬浮颗粒PM2.5浓度超标累计</h2>
      <div class="chart">
        <div id="PM" style="width: 100%; height: 100%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
    <div class="panel" bar>
      <h2>二氧化硫SO2浓度超标累计</h2>
      <div class="chart">
        <div id="SO2" style="width: 100%; height: 100%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
    <div class="panel" bar>
      <h2>一氧化碳CO浓度超标累计</h2>
      <div class="chart">
        <div id="CO" style="width: 100%; height: 100%"></div>
      </div>
      <div class="panel-header"></div>
      <div class="panel-footer"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import request from '@/utils/request8085'

export default {
  data() {
    return {
      //PM
      pmentities: [],
      pmname: [],
      pmvalue: [],
      //CO
      coEntities: [],
      coname: [],
      covalue: [],
      //SO
      so2Entities: [],
      so2name: [],
      so2value: []
    }
  },
  mounted() {
    this.initPM(), this.initSO2(), this.initCO(), this.initEcharts()
  },
  methods: {
    initEcharts() {
      request.get('/stat/query').then((res) => {
        if (res.code === 200) {
          this.pmentities = res.data.pmentities
          this.pmname = this.pmentities.map((item) => item.name)
          this.pmvalue = this.pmentities.map((item) => item.value)
          this.initPM(this.pmname, this.pmvalue)
          this.coEntities = res.data.coEntities
          this.coname = this.coEntities.map((item) => item.name)
          this.covalue = this.coEntities.map((item) => item.value)
          this.initCO(this.coname, this.covalue)
          this.so2Entities = res.data.so2Entities
          this.so2name = this.so2Entities.map((item) => item.name)
          this.so2value = this.so2Entities.map((item) => item.value)
          this.initSO2(this.so2name, this.so2value)
        }
      })
    },
    initPM(xAxis, yAxis) {
      let chartDom = document.getElementById('PM')
      let myChart = echarts.init(chartDom)
      let option

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: xAxis,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: yAxis,
            color: '#93c5fd'
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initSO2(xAxis, yAxis) {
      let chartDom = document.getElementById('SO2')
      let myChart = echarts.init(chartDom)
      let option

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: xAxis,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: yAxis,
            color: '#93c5fd'
          }
        ]
      }

      option && myChart.setOption(option)
    },
    initCO(xAxis, yAxis) {
      let chartDom = document.getElementById('CO')
      let myChart = echarts.init(chartDom)
      let option

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: xAxis,
            axisTick: {
              alignWithLabel: true
            },
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            axisLabel: {
              color: '#f8fafc'
            }
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: yAxis,
            color: '#93c5fd'
          }
        ]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>

<style scoped>
.panel {
  position: relative;
  height: 310px;
  border: 1px solid rgba(25, 186, 139, 0.17);
  background-image: url('@/assets/images/line.png');
  padding: 0 15px 40px;
  margin-bottom: 15px;
}
.panel::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(255, 255, 255, 0.04);
  pointer-events: none;
}
h2 {
  height: 48px;
  line-height: 48px;
  text-align: center;
  color: #fff;
  font-size: 20px;
  font-weight: 400;
}
.chart {
  height: 240px;
}
.panel-header {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
}
.panel-header::before {
  position: absolute;
  content: '';
  top: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-left: 2px solid #02a6b5;
  border-top: 2px solid #02a6b5;
}
.panel-header::after {
  position: absolute;
  content: '';
  top: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-right: 2px solid #02a6b5;
  border-top: 2px solid #02a6b5;
}
.panel-footer {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
}
.panel-footer::before {
  position: absolute;
  content: '';
  bottom: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-left: 2px solid #02a6b5;
  border-bottom: 2px solid #02a6b5;
}
.panel-footer::after {
  position: absolute;
  content: '';
  bottom: 0;
  right: 0;
  width: 10px;
  height: 10px;
  border-right: 2px solid #02a6b5;
  border-bottom: 2px solid #02a6b5;
}
</style>
